#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int main(){
    FILE* file = fopen("test2.txt", "a+");
    if(file == NULL){
        printf("Error opening file\n");
        return 1;
    }

    char buff[1024] ={0};
    int line_num = 0;
    while(fgets(buff, 1024, file) != NULL){
        if(buff[strlen(buff)-1] == '\n'){
            line_num++;    
        }
    }

    printf("line num is %d\n", line_num);
    
    while(1){
        time_t t;
        //返回1970年以来的秒数
        int time_seconds = time(&t);
        //将time_seconds转化为本地时间
        struct tm* time_info = localtime(&t);
        
        line_num++;
        fprintf(file, "%d,%d-%02d-%02d %02d:%02d:%02d\n", line_num, time_info->tm_year+1900,
        time_info->tm_mon+1, time_info->tm_mday, time_info->tm_hour, 
        time_info->tm_min, time_info->tm_sec);
        fflush(file);
        sleep(1);
    }

    fclose(file);
    return 0;
}